package com.hq.ims.data.aop;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hq.authority.utils.AppUtils;
import com.hq.ims.data.entity.LogSysAccess;
import com.hq.utils.util.BaseConstant;
import com.hq.utils.util.StringUtils;
import com.hq.web.inf.user.ITokenUserInfo;
import com.hq.web.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 用户操作aop
 */
@Aspect
@Component
@Slf4j
public class SysAccessLogAop {

    @Resource
    private ApplicationContext applicationContext;

    @Pointcut(value = "execution(* com.hq.ims..controller.*.*(..))")
    private void controllerAspect() {
    }

    @Before(value = "controllerAspect()")
    public void recordLog(JoinPoint joinPoint) throws IOException {
        String uri = WebUtils.getUrl();
        //获取目标类
        Class clazz = joinPoint.getTarget().getClass();
        //获取目标方法名称
        String methodName = joinPoint.getSignature().getName();
        //获取目标方法参数
        Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes();
        //获取req
        HttpServletRequest req = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        //req输入流获取到前端输入参数
        BufferedReader stream = new BufferedReader(new InputStreamReader(req.getInputStream(), BaseConstant.UTF8_STR));
        StringBuilder sb = new StringBuilder();
        //临时搬运变量
        String tempStr = null;
        //标识str是否包含Array
        while ((tempStr = stream.readLine()) != null) {
            sb.append(tempStr);
        }
        String parameter = new String();
        if (StringUtils.isNotEmpty(sb.toString()) && sb.charAt(0) == '[') {
            JSONArray objects = JSONObject.parseArray(sb.toString());
            parameter = objects.toString();
        } else if (StringUtils.isNotEmpty(sb.toString())) {
            parameter = sb.toString();
        }

        LogSysAccess logSysAccess = new LogSysAccess();
        logSysAccess.setClassName(clazz.getName());
        logSysAccess.setUrl(uri);
        logSysAccess.setMetName(methodName);
        logSysAccess.setPackageName(clazz.getPackage().getName());
        logSysAccess.setParams(parameter);
        logSysAccess.setExecutionTime(-1.0);
        ITokenUserInfo currentUserNoException = AppUtils.getCurrentUserNoException();
        String userAccount = StringUtils.EMPTY;
        if (currentUserNoException != null) {
            userAccount = currentUserNoException.getAccount();
        }
        logSysAccess.setCreateUserAccount(userAccount);
        logSysAccess.insert();
    }
}
